import router from '@/router'
const menu = {
    namespaced: true,
	state: {
		menuList: [],
	},
	mutations: {
		SAVE_MENU(state, list) {
			state.menuList = list
		},
	},
	actions: {
		saveMenu({ commit }, list) {
			commit('SAVE_MENU',list)
			// let menuRouesList =  formateMenuList(list)
			// console.log(menuRouesList);
			// 添加动态路由
			// menuRouesList.forEach(route => router.addRoute(route))
		},
	},
	getters: {
		menuList: state => state.menuList,
	},
}

/**
 * 添加动态路由
 *  路由菜单接口数据映射成路由格式数据
 */
function formateMenuList(menuRoutes){
	let newMenuRoutes = []
	menuRoutes.forEach(route => {
		// 如果有子路由，递归调用函数本身，继续映射
		let children
		if (route.children && route.children instanceof Array) {
			children = formateMenuList(route.children)
		}
		const { path, label, meta, iconCls, hidden } = route

		let newRoute = {
			path,
			label,
			meta,
			iconCls,
			hidden,
			children
		}
		// 组件映射
		if (route.path == '/main') {
			newRoute = {
				...newRoute,
				/* component: resolve =>
					require([`@/views/home`], resolve) */
			}
		} else {
			newRoute = {
				...newRoute,
				// component: resolve =>
				// 	require([`@/views/platform${route.path}`], resolve)
				component: ()=>Promise.resolve(require(`@/views/platform${route.path}`).default) //router4版本
			}
		}

		newMenuRoutes.push(newRoute)
	})
	return newMenuRoutes
}


export default menu
